* --------------------- *
* This file loads the child level data to compute 
* the alternative specifications presented in Tables B1 and B2.
* Author: Victor Ronda
* Date Created: 01 Mar 2020
* Last Update: 22 Jan 2023
* --------------------- *


clear all
set more off

**************************
*** Health and Dominance Measures ***
**************************

use "offspring_data.dta", replace

rename health_issue_*_10m3y hi_*_13
rename dominance* dom*
rename dom_relative_rank dom_pct


replace hi_m_13=1-hi_m_13
replace hi_m_13=. if obs_10m3y<=5

xtile mcoh1=mom_cohort if hi_m_13 ~=.,n(20)
xtile coh1=cohort if hi_m_13 ~=.,n(20)

local controls1 "coh1 female primip obs_10m3y"
local controls2 "mcoh1 mom_primip coh1 female primip"
local controls3 "mcoh1 mom_primip coh1 female primip obs_10m3y"

xtile mcoh3=mom_cohort if dom_pct ~=.,n(20)
xtile coh3=cohort if dom_pct ~=.,n(20)

local controls5 "coh3 female primip agedom"
local controls6 "mcoh3 mom_primip coh3 female primip"
local controls7 "mcoh3 mom_primip coh3 female primip agedom"

keep if rear_inte~=.
gen treat0=(rear_inte==0)
gen treat1=(rear_inte==1)
gen treat2=(rear_inte==2)
gen treat3=(rear_inte==3)

keep if rear_inte~=.
foreach var of varlist  hi_m_13 dom_pct{
gen a=0
replace a=1 if `var'~=.
logit a cohort female mom_cohort mom_primip primip
predict ahat_`var'
drop a
}



**** Estimation of Different Treatment Effects ***
foreach var of varlist hi_m_13 {
eststo clear
qui eststo r3_1_`var': teffects ra (`var') (rear_inte), vce(cl mom_id) control((S)PR - Mother (S)PR)
qui eststo r2_1_`var': teffects ra (`var') (rear_inte), vce(cl mom_id) control((S)PR - Mother MR)
qui eststo r1_1_`var': teffects ra (`var') (rear_inte), vce(cl mom_id) control(MR - Mother (S)PR)

qui eststo r3_6_`var': reg `var' treat0 treat1 treat2 `controls3', vce(cl mom_id) 
qui eststo r2_6_`var': reg `var' treat0 treat1 treat3 `controls3',  vce(cl mom_id)  
qui eststo r1_6_`var': reg `var' treat0 treat2 treat3 `controls3', vce(cl mom_id) 

qui eststo r3_2_`var': teffects ra (`var' `controls3') (rear_inte), vce(cl mom_id) control((S)PR - Mother (S)PR)
qui eststo r2_2_`var': teffects ra (`var' `controls3') (rear_inte), vce(cl mom_id) control((S)PR - Mother MR)
qui eststo r1_2_`var': teffects ra (`var' `controls3') (rear_inte), vce(cl mom_id) control(MR - Mother (S)PR)

qui eststo r3_3_`var': teffects ipw (`var') (rear_inte `controls2'), vce(cl mom_id) control((S)PR - Mother (S)PR)
qui eststo r2_3_`var': teffects ipw (`var') (rear_inte `controls2'), vce(cl mom_id) control((S)PR - Mother MR)
qui eststo r1_3_`var': teffects ipw (`var') (rear_inte `controls2'), vce(cl mom_id) control(MR - Mother (S)PR)

qui eststo r3_4_`var': teffects aipw (`var' `controls1') (rear_inte `controls2'), vce(cl mom_id) control((S)PR - Mother (S)PR)
qui eststo r2_4_`var': teffects aipw (`var' `controls1') (rear_inte `controls2'), vce(cl mom_id) control((S)PR - Mother MR)
qui eststo r1_4_`var': teffects aipw (`var' `controls1') (rear_inte `controls2'), vce(cl mom_id) control(MR - Mother (S)PR)

qui eststo r3_5_`var': teffects ipw (`var') (rear_inte `controls2' ahat_`var'), vce(cl mom_id) control((S)PR - Mother (S)PR)
qui eststo r2_5_`var': teffects ipw (`var') (rear_inte `controls2' ahat_`var'), vce(cl mom_id) control((S)PR - Mother MR)
qui eststo r1_5_`var': teffects ipw (`var') (rear_inte `controls2' ahat_`var'), vce(cl mom_id) control(MR - Mother (S)PR)


qui teffects ra (`var') (rear_inte), vce(cl mom_id) pom
qui eststo rdif_1_`var': nlcom _b[POmeans:0.rear_inte]+_b[POmeans:3.rear_inte]-_b[POmeans:2.rear_inte]-_b[POmeans:1.rear_inte], post

qui reg `var' treat0 treat1 treat2 `controls3', vce(cl mom_id)   
qui eststo rdif_6_`var': nlcom _b[treat0]-_b[treat1]-_b[treat2], post


qui teffects ra (`var' `controls3') (rear_inte), vce(cl mom_id) pom
qui eststo rdif_2_`var': nlcom _b[POmeans:0.rear_inte]+_b[POmeans:3.rear_inte]-_b[POmeans:2.rear_inte]-_b[POmeans:1.rear_inte], post
qui teffects ipw (`var') (rear_inte `controls2'), vce(cl mom_id) pom
qui eststo rdif_3_`var': nlcom _b[POmeans:0.rear_inte]+_b[POmeans:3.rear_inte]-_b[POmeans:2.rear_inte]-_b[POmeans:1.rear_inte], post
qui teffects aipw (`var' `controls1') (rear_inte `controls2'), vce(cl mom_id) pom
qui eststo rdif_4_`var': nlcom _b[POmeans:0.rear_inte]+_b[POmeans:3.rear_inte]-_b[POmeans:2.rear_inte]-_b[POmeans:1.rear_inte], post
qui teffects ipw (`var') (rear_inte `controls2' ahat_`var'), vce(cl mom_id) pom
qui eststo rdif_5_`var': nlcom _b[POmeans:0.rear_inte]+_b[POmeans:3.rear_inte]-_b[POmeans:2.rear_inte]-_b[POmeans:1.rear_inte], post

}



foreach var of varlist dom_pct {
qui eststo r3_1_`var': teffects ra (`var') (rear_inte), vce(cl mom_id) control((S)PR - Mother (S)PR)
qui eststo r2_1_`var': teffects ra (`var') (rear_inte), vce(cl mom_id) control((S)PR - Mother MR)
qui eststo r1_1_`var': teffects ra (`var') (rear_inte), vce(cl mom_id) control(MR - Mother (S)PR)

qui eststo r3_6_`var': reg `var' treat0 treat1 treat2 `controls7', vce(cl mom_id) 
qui eststo r2_6_`var': reg `var' treat0 treat1 treat3 `controls7', vce(cl mom_id)  
qui eststo r1_6_`var': reg `var' treat0 treat2 treat3 `controls7', vce(cl mom_id)  

qui eststo r3_2_`var': teffects ra (`var' `controls7') (rear_inte), vce(cl mom_id) control((S)PR - Mother (S)PR)
qui eststo r2_2_`var': teffects ra (`var' `controls7') (rear_inte), vce(cl mom_id) control((S)PR - Mother MR)
qui eststo r1_2_`var': teffects ra (`var' `controls7') (rear_inte), vce(cl mom_id) control(MR - Mother (S)PR)

qui eststo r3_3_`var': teffects ipw (`var') (rear_inte `controls6'), vce(cl mom_id) control((S)PR - Mother (S)PR)
qui eststo r2_3_`var': teffects ipw (`var') (rear_inte `controls6'), vce(cl mom_id) control((S)PR - Mother MR)
qui eststo r1_3_`var': teffects ipw (`var') (rear_inte `controls6'), vce(cl mom_id) control(MR - Mother (S)PR)

qui eststo r3_4_`var': teffects aipw (`var' `controls5') (rear_inte `controls6'), vce(cl mom_id) control((S)PR - Mother (S)PR)
qui eststo r2_4_`var': teffects aipw (`var' `controls5') (rear_inte `controls6'), vce(cl mom_id) control((S)PR - Mother MR)
qui eststo r1_4_`var': teffects aipw (`var' `controls5') (rear_inte `controls6'), vce(cl mom_id) control(MR - Mother (S)PR)

qui eststo r3_5_`var': teffects ipw (`var') (rear_inte `controls6' ahat_`var'), vce(cl mom_id) control((S)PR - Mother (S)PR)
qui eststo r2_5_`var': teffects ipw (`var') (rear_inte `controls6' ahat_`var'), vce(cl mom_id) control((S)PR - Mother MR)
qui eststo r1_5_`var': teffects ipw (`var') (rear_inte `controls6' ahat_`var'), vce(cl mom_id) control(MR - Mother (S)PR)


qui teffects ra (`var') (rear_inte), vce(cl mom_id) pom   
qui eststo rdif_1_`var': nlcom _b[POmeans:0.rear_inte]+_b[POmeans:3.rear_inte]-_b[POmeans:2.rear_inte]-_b[POmeans:1.rear_inte], post

qui reg `var' treat0 treat1 treat2 `controls7', vce(cl mom_id)
qui eststo rdif_6_`var': nlcom _b[treat0]-_b[treat1]-_b[treat2], post

qui teffects ra (`var' `controls7') (rear_inte), vce(cl mom_id) pom
qui eststo rdif_2_`var': nlcom _b[POmeans:0.rear_inte]+_b[POmeans:3.rear_inte]-_b[POmeans:2.rear_inte]-_b[POmeans:1.rear_inte], post
qui teffects ipw (`var') (rear_inte `controls6'), vce(cl mom_id) pom
qui eststo rdif_3_`var': nlcom _b[POmeans:0.rear_inte]+_b[POmeans:3.rear_inte]-_b[POmeans:2.rear_inte]-_b[POmeans:1.rear_inte], post
qui teffects aipw (`var' `controls5') (rear_inte `controls6'), vce(cl mom_id) pom
qui eststo rdif_4_`var': nlcom _b[POmeans:0.rear_inte]+_b[POmeans:3.rear_inte]-_b[POmeans:2.rear_inte]-_b[POmeans:1.rear_inte], post
qui teffects ipw (`var') (rear_inte `controls6' ahat_`var'), vce(cl mom_id) pom
qui eststo rdif_5_`var': nlcom _b[POmeans:0.rear_inte]+_b[POmeans:3.rear_inte]-_b[POmeans:2.rear_inte]-_b[POmeans:1.rear_inte], post

}



**** Table B1 ****


foreach var of varlist hi_m_13 {
estout r3_*_`var' , rename(treat1 r1vs3.rear_inte) keep(r1vs3.rear_inte)  cells(b(fmt(3)) p(par([ ]) fmt(3))) label style(tex) starlevels( * 0.1 ** 0.05 *** 0.01) replace collabels(,none) mlabels("" "" "" "" "" "") eqlabel("" "") varlabels(r1vs3.rear_inte "$\Psi(s_{0,1})-\Psi(s_{0,0})$" )
estout r2_*_`var' , rename(treat0 r0vs2.rear_inte) keep(r0vs2.rear_inte)  cells(b(fmt(3)) p(par([ ]) fmt(3))) label style(tex) starlevels( * 0.1 ** 0.05 *** 0.01) replace collabels(,none) mlabels("" "" "" "" "" "") eqlabel("" "") varlabels(r0vs2.rear_inte "$\Psi(s_{1,1})-\Psi(s_{1,0})$" )
estout r3_*_`var' , rename(treat2 r2vs3.rear_inte) keep(r2vs3.rear_inte)  cells(b(fmt(3)) p(par([ ]) fmt(3))) label style(tex) starlevels( * 0.1 ** 0.05 *** 0.01) replace collabels(,none) mlabels("" "" "" "" "" "") eqlabel("" "") varlabels(r2vs3.rear_inte "$\Psi(s_{1,0})-\Psi(s_{0,0})$" )
estout r1_*_`var' , rename(treat0 r0vs1.rear_inte) keep(r0vs1.rear_inte)  cells(b(fmt(3)) p(par([ ]) fmt(3))) label style(tex) starlevels( * 0.1 ** 0.05 *** 0.01) replace collabels(,none) mlabels("" "" "" "" "" "") eqlabel("" "") varlabels(r0vs1.rear_inte "$\Psi(s_{1,1})-\Psi(s_{0,1})$" )
estout r1_*_`var' , rename(treat2 r2vs1.rear_inte) keep(r2vs1.rear_inte)  cells(b(fmt(3)) p(par([ ]) fmt(3))) label style(tex) starlevels( * 0.1 ** 0.05 *** 0.01) replace collabels(,none) mlabels("" "" "" "" "" "") eqlabel("" "") varlabels(r2vs1.rear_inte "$\Psi(s_{1,0})-\Psi(s_{0,1})$" )
estout r3_*_`var' , rename(treat0 r0vs3.rear_inte) keep(r0vs3.rear_inte)  cells(b(fmt(3)) p(par([ ]) fmt(3))) label style(tex) starlevels( * 0.1 ** 0.05 *** 0.01) replace collabels(,none) mlabels("" "" "" "" "" "") eqlabel("" "") varlabels(r0vs3.rear_inte "$\Psi(s_{1,1})-\Psi(s_{0,0})$" )
estout rdif_*_`var' , cells(b(fmt(3)) p(par([ ]) fmt(3))) label style(tex) starlevels( * 0.1 ** 0.05 *** 0.01) replace collabels(,none) mlabels("" "" "" "" "" "") eqlabel("" "") varlabels(_nl_1 "$ [\Psi(s_{1,1})-\Psi(s_{0,1})] -[\Psi(s_{1,0})-\Psi(s_{0,0})] $" )
}

**** Table B2 ****
foreach var of varlist dom_pct {
estout r3_*_`var' , rename(treat1 r1vs3.rear_inte) keep(r1vs3.rear_inte)  cells(b(fmt(3)) p(par([ ]) fmt(3))) label style(tex) starlevels( * 0.1 ** 0.05 *** 0.01) replace collabels(,none) mlabels("" "" "" "" "" "") eqlabel("" "") varlabels(r1vs3.rear_inte "$\Psi(s_{0,1})-\Psi(s_{0,0})$" )
estout r2_*_`var' , rename(treat0 r0vs2.rear_inte) keep(r0vs2.rear_inte)  cells(b(fmt(3)) p(par([ ]) fmt(3))) label style(tex) starlevels( * 0.1 ** 0.05 *** 0.01) replace collabels(,none) mlabels("" "" "" "" "" "") eqlabel("" "") varlabels(r0vs2.rear_inte "$\Psi(s_{1,1})-\Psi(s_{1,0})$" )
estout r3_*_`var' , rename(treat2 r2vs3.rear_inte) keep(r2vs3.rear_inte)  cells(b(fmt(3)) p(par([ ]) fmt(3))) label style(tex) starlevels( * 0.1 ** 0.05 *** 0.01) replace collabels(,none) mlabels("" "" "" "" "" "") eqlabel("" "") varlabels(r2vs3.rear_inte "$\Psi(s_{1,0})-\Psi(s_{0,0})$" )
estout r1_*_`var' , rename(treat0 r0vs1.rear_inte) keep(r0vs1.rear_inte)  cells(b(fmt(3)) p(par([ ]) fmt(3))) label style(tex) starlevels( * 0.1 ** 0.05 *** 0.01) replace collabels(,none) mlabels("" "" "" "" "" "") eqlabel("" "") varlabels(r0vs1.rear_inte "$\Psi(s_{1,1})-\Psi(s_{0,1})$" )
estout r1_*_`var' , rename(treat2 r2vs1.rear_inte) keep(r2vs1.rear_inte)  cells(b(fmt(3)) p(par([ ]) fmt(3))) label style(tex) starlevels( * 0.1 ** 0.05 *** 0.01) replace collabels(,none) mlabels("" "" "" "" "" "") eqlabel("" "") varlabels(r2vs1.rear_inte "$\Psi(s_{1,0})-\Psi(s_{0,1})$" )
estout r3_*_`var' , rename(treat0 r0vs3.rear_inte) keep(r0vs3.rear_inte)  cells(b(fmt(3)) p(par([ ]) fmt(3))) label style(tex) starlevels( * 0.1 ** 0.05 *** 0.01) replace collabels(,none) mlabels("" "" "" "" "" "") eqlabel("" "") varlabels(r0vs3.rear_inte "$\Psi(s_{1,1})-\Psi(s_{0,0})$" )
estout rdif_*_`var' , cells(b(fmt(3)) p(par([ ]) fmt(3))) label style(tex) starlevels( * 0.1 ** 0.05 *** 0.01) replace collabels(,none) mlabels("" "" "" "" "" "") eqlabel("" "") varlabels(_nl_1 "$ [\Psi(s_{1,1})-\Psi(s_{0,1})] -[\Psi(s_{1,0})-\Psi(s_{0,0})] $" )
}
















